package com.mp.service;

import com.mp.aspect.CatAnnotation;
import com.mp.dao.mapper.UserAccountDao;
import com.mp.dao.po.UserAccountDO;
import com.mp.domain.response.WelcomeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Random;
import java.util.UUID;

@Slf4j
@Service
public class MediaService {

    @Autowired
    private UserAccountDao userAccountDao;

    @CatAnnotation
    public WelcomeVo getWelcomeInformation(String uuid){
        WelcomeVo vo = new WelcomeVo();

        vo.setUuid(uuid);
        vo.setUid("100001");
        vo.setName("李湘");
        vo.setAddress("西安市雁塔区");

        log.error(vo.toString());

        return vo;
    }

    @Transactional(rollbackFor = Exception.class)
    public WelcomeVo doTransaction(String uuid){
        WelcomeVo vo = new WelcomeVo();

        vo.setUuid(uuid);
        vo.setUid("100001transaction");
        vo.setName("李湘 do transaction");
        vo.setAddress("西安市雁塔区 do transaction");

        log.error(vo.toString());

        vo = getExceptionInfo(uuid);

        return vo;
    }


    public WelcomeVo getExceptionInfo(String uuid){
        WelcomeVo vo = new WelcomeVo();

        vo.setUuid(uuid);
        vo.setUid("100001ERROR");
        vo.setName("100001ERROR111");
        vo.setAddress("100001ERROR1111");
        Random rand=new Random();
        try {
            int res = 9/rand.nextInt(3);
        } catch (Exception ex) {
            log.error("getExceptionInfo---> has exception : {}", ex);
        }
        return vo;
    }


    public WelcomeVo queryInfoById(Long id){
        UserAccountDO userAccount = userAccountDao.get(id);
        if(userAccount == null){
            return null;
        }
        WelcomeVo vo = new WelcomeVo();

        vo.setUuid(UUID.randomUUID().toString());
        vo.setUid(userAccount.getId().toString());
        vo.setName(userAccount.getUserName());
        vo.setAddress(userAccount.getAddress());

        return vo;
    }
}
